/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testsUnitaires;

import Controller.*;
import Model.MaterielDAO;
import java.sql.Connection;
import java.sql.Date;

/**
 *
 * @author Romain
 */
public class testCodeBarre {
    
    private Materiel mat;
    private Long numCB;
    private Integer modulo;
    private Integer checkDigit;
    private String codeBarreTemp;
    private Connection connect;
    
    public testCodeBarre(Materiel mat,Connection connect){
        this.mat = mat;
        this.connect = connect;
    }
    
    public Long creerCodeBarre(){
        if (this.mat.getSecteur() != null && this.mat.getDateHT() != null && this.mat.getIdT() != null) {
            
            codeBarreTemp = String.valueOf(this.mat.getSecteur().getId()) + String.valueOf(this.mat.getDateHT().getYear()) + String.valueOf(this.mat.getIdT() + new MaterielDAO(this.connect).getIdCb());
            
            String[] mesNum = codeBarreTemp.split("");

            modulo = (Integer.valueOf(mesNum[0]) + (Integer.valueOf(mesNum[1]) * 3) + Integer.valueOf(mesNum[2]) + (Integer.valueOf(mesNum[3]) * 3) + Integer.valueOf(mesNum[4]) + (Integer.valueOf(mesNum[5]) * 3) + Integer.valueOf(mesNum[6]) + (Integer.valueOf(mesNum[7]) * 3) + Integer.valueOf(mesNum[8]) + (Integer.valueOf(mesNum[9]) * 3) + Integer.valueOf(mesNum[10]) + (Integer.valueOf(mesNum[11]) * 3)) % 10;
            checkDigit = 10 - modulo;
            codeBarreTemp = codeBarreTemp + checkDigit.toString();
            
            numCB = Long.valueOf(codeBarreTemp);
            
            System.out.println(numCB);
            return  numCB;
        }
        else {
            System.out.println(" >>>>>>  Vérifiez que les champs de ce matériel sont bien tous remplis !!  <<<<<<");
        
            return  null;
        }
    }
    
}
